package com.battle.service.repository.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.battle.pojo.entity.QuestionBankDetails;
import com.battle.pojo.enums.QuestionBankTypeEnum;
import com.battle.service.mapper.QuestionBankDetailsMapper;
import com.battle.service.repository.in.QuestionBankDetailsRepository;
import nirvana.core.repository.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;
    import com.battle.pojo.dto.questionBankDetails.*;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
* 【描 述】：
* 【环 境】：J2SE
* 【详 细】：
*
*/
@Service
public class QuestionBankDetailsRepositoryImpl extends Repository< QuestionBankDetailsMapper , QuestionBankDetails> implements QuestionBankDetailsRepository {

private final QuestionBankDetailsMapper mapper;

public QuestionBankDetailsRepositoryImpl(QuestionBankDetailsMapper mapper) {
this.mapper = mapper;
}

    /**
    * 搜索题目管理
    * @param dto
    * @return
    */
    @Override
    public Page< QuestionBankDetails > selectQuestionBankDetails(SelectQuestionBankDetailsDto dto){

    return mapper.selectQuestionBankDetails(dto);
    }

@Override
public  Map<Long, QuestionBankDetails> getMap(List<Long> ids){
    Map<Long, QuestionBankDetails> map = new HashMap<>();

    if (CollectionUtil.isNotEmpty(ids)) {
        List<QuestionBankDetails> list =listByIds(ids);
        if (CollectionUtil.isNotEmpty(list)) {
        map = list.stream().collect(Collectors.toMap(QuestionBankDetails::getId, pojo -> pojo));
         }
        }
        return map;
    }

    @Override
    public Map<Long, List<QuestionBankDetails>> getMapListByBankIds(List<Long> bankIds) {
        List<QuestionBankDetails> detailsList = mapper.selectList(new LambdaQueryWrapper<QuestionBankDetails>()
                .in(QuestionBankDetails::getQuestionBankId, bankIds)
                .eq(QuestionBankDetails::getIsDelete, 0));
        if (CollectionUtil.isNotEmpty(detailsList)) {
            return detailsList.stream().collect(Collectors.groupingBy(QuestionBankDetails::getQuestionBankId));
        }
        return new HashMap<>();
    }

    @Override
    public List<QuestionBankDetails> getListByBankId(List<Long> bankIds, QuestionBankTypeEnum type,Integer number) {
        List<QuestionBankDetails> list=new ArrayList<>();
        List<QuestionBankDetails> detailsList = mapper.selectList(new LambdaQueryWrapper<QuestionBankDetails>()
                .in(QuestionBankDetails::getQuestionBankId, bankIds)
                .eq(QuestionBankDetails::getType, type)
                .eq(QuestionBankDetails::getIsDelete, 0));
        if (CollectionUtil.isNotEmpty(detailsList)) {
            //随机取指定数量的题目
            for (int i = 0; i < number; i++) {
                //随机从detailsList中取一个
                int index = (int) (Math.random() * detailsList.size());
                list.add(detailsList.get(index));
            }
        }
        return list;
    }

    /**
     * 根据题库id获关联题目
     * @param bankId
     * @return
     */
    @Override
    public List<QuestionBankDetails> getListByBankId(Long bankId) {
        return  mapper.selectList(new LambdaQueryWrapper<QuestionBankDetails>()
                .eq(QuestionBankDetails::getQuestionBankId, bankId)
                .eq(QuestionBankDetails::getIsDelete, 0));
    }
}